<?php
namespace Home\Controller;
use Think\Controller;

class CustomizedController extends FrontController {
	public function buy() {
		if (!session('用户')) {
			session('afterLogin', U('Customized/buy'));
			$this->redirect('User/login',['afterLogin'=>'1']);
		}
		//sui
		$this->assign('title', '定制旅游');
		$sui = [
			'information' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);
		//用户信息
		$user = D('User')->find(session('用户')['id']);
		$this->assign('user', $user);
		//地区信息
		$region = D('Region')->select();
		$this->assign('region', $region);
		//众筹类型
		$type = D('FundingType')->select();
		$this->assign('fundingType', $type);
		if (IS_POST) {
			//判断游客数量
			if (count($_POST['tourist']) == 0) {
				$this->error('至少选择一个游客!');
			}
			//计算保险价格
			$insurancePrice = 0;
			foreach ($_POST['insurance'] as $insuranceId) {
				$insurance = D('Insurance')->find($insuranceId);
				$insurancePrice += $insurance['price'] * count($_POST['tourist']);
			}
			$_POST['insurance_price'] = $insurancePrice;
			$_POST['user_id'] = session('用户')['id'];
			
			$model = D('CustomizedTravel');
			$type = $_POST['type'];
			if (!$model->create()) {
				$this->error($model->getError());
				return false;
			}
			$newId = $model->add();
			//保险订单
			foreach ($_POST['tourist'] as $touristId) {
				$tourist = D('Tourist')->find($touristId);
				unset($data);
				$data['user_id'] = session('用户')['id'];
				$data['tourist_id'] = $touristId;
				$data['order_type'] = 'customized_travel';
				$data['order_id'] = $newId;
				D('TouristOrder')->data($data)->add();
				foreach ($_POST['insurance'] as $insuranceId) {
					unset($data);
					$data['user_id'] = session('用户')['id'];
					$data['insurance_id'] = $insuranceId;
					$data['order_type'] = 'customized_travel';
					$data['order_id'] = $newId;
					$data['name'] = $tourist['name'];
					$data['phone'] = $tourist['phone'];
					$data['id_number'] = $tourist['id_number'];
					D('InsuranceOrder')->data($data)->add();
				}
			}

			//提示
			notice(session('用户')['id'], 1, '收到新的定制旅游订单', '收到新的定制旅游订单，请查看');
			//跳转到提示页面
			$this->redirect('/Home/Customized/CustimizedSuccess/type/'.$type);
		}
		$this->display();
	}

	public function CustimizedSuccess($type = 1) {
		//用户信息
		$user = D('User')->find(session('用户')['id']);
		$this->assign('user', $user);
		//sui
		$this->assign('title', '定制成功');
		$sui = [
			'information' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);
		//随机选择一个旅行社
		$userClubs = D('UserClub')->where(['bussiness'=>['LIKE', '%'.$type.'%']])->select();
		$randKey = rand(1,count($userClubs)-1);
		$this->assign('tel', $userClubs[$randKey]['tel']);
		$this->display();
	}

    public function pay($id) {
        $customizedTravel = D('CustomizedTravel')->find($id);
        $funding=D('Funding')->find($customizedTravel['funding_id']);
        $price = $funding['customized_price']+$customizedTravel['insurance_price'];
        if (!$this->isWeixin()) {
            $this->redirect('/Api/Alipay/pay', ['out_trade_no'=>'customizedTravel-'.$id, 'subject'=>$funding['name'], 'total_fee'=>$price]);
        } else {
            $out_trade_no = 'customizedTravel-'.$id;
            $subject = $funding['name'];
            $total_fee = $price;
            session('wxpay_out_trade_no', $out_trade_no);
            session('wxpay_subject', $subject);
            session('wxpay_total_fee', $total_fee);
            $this->redirect('/Api/Wxpay/pay');
        }
    }
}
